package com.lgd.threadtest;

/**
 * Created by 299174105 on 2017/8/10.
 */
public class ThreadTest {
    private ThreadTest() {
    }
    //懒汉式单例模式加锁
    private static ThreadTest threadTest = null;
    public static ThreadTest getThreadTest(){
        /*如果第一个线程获取到了单例的实例对象，
         * 后面的线程再获取实例的时候不需要进入同步代码块中了*/
        if(null == threadTest){
         //同步代码块用的锁是单例的字节码文件对象，且只能用这个锁
            synchronized (ThreadTest.class){
                if (null == threadTest){
                    new ThreadTest();
                }
            }
        }
    return threadTest;
    }
    public static void main(String[] args) {
        Thread thread = new Thread(() -> System.out.println("runnable run")) {
            @Override
            public void run() {
                System.out.println("thread run");
            }
        };
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ThreadTest.getThreadTest();
    }
}
